# $Id: Makefile 104 2011-10-21 15:19:31Z cedric.shih@gmail.com $
#
# Copyright 2011 Cedric Shih (cedric dot shih at gmail dot com)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#

## project metadata

NAME = niortsp
MAJOR_VER = 1
MINOR_VER = 5
PATCH_VER = 0

## toolchain

CC = $(CROSS_COMPILE)gcc
AR = $(CROSS_COMPILE)ar
STRIP = $(CROSS_COMPILE)strip

MKDIR = mkdir -p
INSTALL = install
RM = rm -f
LN = ln -sf

## build flags

DESTDIR =
PREFIX = /usr/local

CPPFLAGS = -g -O
INCLUDES = -Isrc -Iapi -Icompat -I$(DESTDIR)$(PREFIX)/include

CFLAGS = -fPIC -Wall $(CPPFLAGS) $(INCLUDES)
LDFLAGS = -L$(DESTDIR)$(PREFIX)/lib
ARFLAGS = rcs

## prerequsites

## artifacts

SRCS = \
	src/codec/headers.c \
	src/codec/request.c \
	src/daemon.c \
	src/h264.c \
	src/handlers.c \
	src/headers.c \
	src/jpeg.c \
	src/listener.c \
	src/log.c \
	src/media.c \
	src/request.c \
	src/rtp.c \
	src/server.c \
	src/session.c \
	src/sessions.c \
	src/stream.c \
	src/track.c \
	src/transport.c \
	src/url.c \
	src/version.c \

OBJS = $(patsubst %.c,%.o,$(SRCS))
DEPS = $(patsubst %.c,%.d,$(SRCS))
SOLIBS = 
ARLIBS =

SOLINK = lib$(NAME).so
SONAME = $(SOLINK).$(MAJOR_VER)
SOREAL = $(SONAME).$(MINOR_VER).$(PATCH_VER)
LIB = lib$(NAME).a

TEST_SRCS = \

TEST_OBJS = $(patsubst %.c,%.o,$(TEST_SRCS))
TEST_DEPS = $(patsubst %.c,%.d,$(TEST_SRCS))
TEST_SOLIBS =
TEST_ARLIBS =

TEST_SOLINK = lib$(NAME)-test.so
TEST_SONAME = $(TEST_SOLINK).$(MAJOR_VER)
TEST_SOREAL = $(TEST_SONAME).$(MINOR_VER).$(PATCH_VER)
TEST_LIB = lib$(NAME)-test.a

API_PREFIX = 
APIS = $(NAME).h
SUBAPIS = h264.h jpeg.h server.h transport.h

LIBS = $(LIB) $(TEST_LIB)
ifneq ($(CROSS_COMPILE),i586-mingw32msvc-)
	LIBS += $(SOREAL) $(TEST_SOREAL)
endif
ARTIFACTS = $(LIBS)

## targets

all: $(ARTIFACTS)

-include $(DEPS) $(TEST_DEPS)

src/version.c: Makefile
	echo "const char *$(subst -,_,$(NAME))_get_version(void) { return \"$(MAJOR_VER).$(MINOR_VER).$(PATCH_VER)\"; }" > $@

$(SOREAL): $(OBJS)
	$(CC) -shared -Wl,-soname,$(SONAME) -o $@ $^

$(LIB): $(OBJS)
	$(AR) $(ARFLAGS) $@ $^

$(TEST_SOREAL): $(TEST_OBJS)
	$(CC) -shared -Wl,-soname,$(TEST_SONAME) -o $@ $^

$(TEST_LIB): $(TEST_OBJS)
	$(AR) $(ARFLAGS) $@ $^

%.o: %.c
	$(CC) $(CFLAGS) -c -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o"$@" "$<"
	
clean:
	rm -f src/version.c $(OBJS) $(DEPS) $(TEST_OBJS) $(TEST_DEPS) $(ARTIFACTS)

install: all
	$(MKDIR) $(DESTDIR)$(PREFIX)/include/$(API_PREFIX)/
	$(INSTALL) -m 0644 $(addprefix api/$(API_PREFIX),$(APIS)) $(DESTDIR)$(PREFIX)/include/$(API_PREFIX)/
	$(MKDIR) $(DESTDIR)$(PREFIX)/include/$(API_PREFIX)/$(NAME)/
	$(INSTALL) -m 0644 $(addprefix api/$(API_PREFIX)/$(NAME)/,$(SUBAPIS)) $(DESTDIR)$(PREFIX)/include/$(API_PREFIX)/$(NAME)/
	$(MKDIR) $(DESTDIR)$(PREFIX)/lib/
	$(INSTALL) -m 0755 $(ARTIFACTS) $(DESTDIR)$(PREFIX)/lib/
	$(LN) -T $(SOREAL) $(DESTDIR)$(PREFIX)/lib/$(SONAME)
	$(LN) -T $(SOREAL) $(DESTDIR)$(PREFIX)/lib/$(SOLINK)
	$(LN) -T $(SOREAL) $(DESTDIR)$(PREFIX)/lib/$(TEST_SONAME)
	$(LN) -T $(SOREAL) $(DESTDIR)$(PREFIX)/lib/$(TEST_SOLINK)

uninstall:
	$(RM) $(addprefix $(DESTDIR)$(PREFIX)/include/$(API_PREFIX)/,$(APIS))
	$(RM) $(addprefix $(DESTDIR)$(PREFIX)/include/$(API_PREFIX)/$(NAME)/,$(SUBAPIS))
	$(RM) $(addprefix $(DESTDIR)$(PREFIX)/lib/,$(ARTIFACTS))
	$(RM) $(DESTDIR)$(PREFIX)/lib/$(SONAME)
	$(RM) $(DESTDIR)$(PREFIX)/lib/$(SOLINK)
	$(RM) $(DESTDIR)$(PREFIX)/lib/$(TEST_SONAME)
	$(RM) $(DESTDIR)$(PREFIX)/lib/$(TEST_SOLINK)

.PHONY: clean all install uninstall
